package com.hmeter.aws.s3;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.Region;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectInputStream;
import com.amazonaws.services.s3.model.S3ObjectSummary;

public class S3SamplerUtility {

	private PropertiesCredentials pc = null;
	private AmazonS3 s3 = null;

	public PropertiesCredentials getPropertiesCredentials()
	{
		return pc;
	}
	
	public AmazonS3 getAmazonS3()
	{
		return s3;
	}
	
	public S3SamplerUtility(String awsCredentialsPropertyPath) throws FileNotFoundException, IllegalArgumentException, IOException 
	{
		PropertiesCredentials pc = new PropertiesCredentials(new File(awsCredentialsPropertyPath));
		s3 = new AmazonS3Client(pc);
	}
	
	public String createNewBucket(String bucketName, String region)
	{
		if (region.equals(""))
		{
			s3.createBucket(bucketName);
			return "Created bucket " + bucketName;
		}
		else
		{
			Region reg = Region.valueOf(region);
			s3.createBucket(bucketName, reg);
			return "Created bucket " + bucketName + " in region " + reg.toString();
		}
		
	}	
	
	public String uploadFileToBucket(String bucketName, String key, String inputFilePath)
	{
		File file = new File(inputFilePath);
        PutObjectRequest putRequest = new PutObjectRequest(bucketName, key, file);
        s3.putObject(putRequest);
        return "Uploaded file" + file.getName() + "to bucket " + bucketName;
	}
	
	public String readFileFromBucket(String bucketName, String key) throws IOException
	{
		StringBuilder strBuilder = new StringBuilder();
		
		GetObjectRequest getRequest = new GetObjectRequest(bucketName, key);
        S3Object s3object = s3.getObject(getRequest);
        S3ObjectInputStream inputStream = s3object.getObjectContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        while (reader.ready())
        {
        	strBuilder.append(reader.readLine());
        }
        reader.close();
		
		return strBuilder.toString();
	}
	
	public String downloadFileFromBucket(String bucketName, String key, String outputFilePath)
	{
		String response = "";
		
		try {
			String content = this.readFileFromBucket(bucketName, key);
			File outputFile = new File(outputFilePath);
	        BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile));
	        writer.write(content);
	        writer.close();
	        response = "File downloaded successfully";
			
		} catch (IOException e) {
			System.out.println(e.getMessage());
			response = "File not downloaded successfully";
		}
		
		return response;
	}
	
	public String getListOfObjectsInBucket(String bucketName)
	{
		StringBuilder response = new StringBuilder();		
		ObjectListing objectsInBucket = s3.listObjects(bucketName);
		
		for (S3ObjectSummary obj : objectsInBucket.getObjectSummaries())
		{
			response.append(obj.getOwner() + " " + obj.getSize() + " " + obj.getLastModified() + " " + obj.getKey());
		}	
		return response.toString();
	}
	
	
	public String deleteObjectFromBucket(String bucketName, String key)
	{
		s3.deleteObject(bucketName, key);
		return "Object deleted from bucket";
	}
	
	public String deleteBucket(String bucketName)
	{
		s3.deleteBucket(bucketName);
		return "Bucket deleted";
	}
}
